#  Copyright (c) 2021- ~, XIWANG
#  
#  OLSR simulation analysis result draw making png
#  Used in Python3
#  Clone from https://gitee.com/WangXi_Chn/olsr_-analysis.git
#  Note in https://www.yuque.com/wangxi_chn
#  
#  Change Logs:
#  Date           Author       Notes			Mail
#  2021-2-21     XiWang   	   first version	WangXi_Chn@foxmail.com

import matplotlib.pyplot as plt
import numpy as np
import datacube as dc
import sys

try:
    INPUT_COMMOND = sys.argv[1]
    if INPUT_COMMOND == "help":
        print("--------------------------------------------------------------------------------------------\n")
        print("     Use Commands     \n")
        print("NAME\n")     
        print("     OLSR simulation analysis result draw making png all in One\n")
        print("DISCRIPTION\n")
        print("     python3 draw_analysis.py\n")
        print("         -v  $speed_num $times_num $node_num $movement_range $movement_MinSpeed $movement_MaxSpeed\n")
        print("         -s  $range_num $times_num $node_num $movement_Speed $movement_MinRange $movement_MaxRange\n")
        print("         -c  $filename_1 $filename_2 $times_num $num_axis_x $num_axis_x_MIN $num_axis_x_MAX\n")
        print("         -t  $node_num $movement_Speed $movement_range\n")
        print("         -tc $filename_1 $filename_2 $node_num $movement_Speed $movement_range\n")
        print("			-cm NUM $times_num $num_axis_x $num_axis_x_MIN $num_axis_x_MAX $FILENAME(s)\n")
        print("			-tcm NUM $node_num $movement_Speed $movement_range $FILENAME(s)\n")
        print("--------------------------------------------------------------------------------------------\n")
        exit()
    elif INPUT_COMMOND == "-v":
        NUM_SPEED = int(sys.argv[2])
        NUM_TIMES = int(sys.argv[3])
        NUM_NODE = int(sys.argv[4])
        NUM_MOVEMENT_RANGE = int(sys.argv[5])
        NUM_MOVEMENT_MINSPEED = int(sys.argv[6])
        NUM_MOVEMENT_MAXSPEED = int(sys.argv[7])
    elif INPUT_COMMOND == "-s":
        NUM_RANGE = int(sys.argv[2])
        NUM_TIMES = int(sys.argv[3])
        NUM_NODE = int(sys.argv[4])
        NUM_MOVEMENT_SPEED = int(sys.argv[5])
        NUM_MOVEMENT_MINRANGE = int(sys.argv[6])
        NUM_MOVEMENT_MAXRANGE = int(sys.argv[7])
    elif INPUT_COMMOND == "-c":
        NAME_FILE_1 = sys.argv[2]
        NAME_FILE_2 = sys.argv[3]
        NUM_TIMES = int(sys.argv[4])
        NUM_AXIS_X = int(sys.argv[5])
        NUM_AXIS_X_MIN = int(sys.argv[6])
        NUM_AXIS_X_MAX = int(sys.argv[7])
    elif INPUT_COMMOND == "-t":
        NUM_NODE = int(sys.argv[2])
        NUM_MOVEMENT_SPEED = int(sys.argv[3])
        NUM_MOVEMENT_RANGE = int(sys.argv[4])
    elif INPUT_COMMOND == "-tc":
        NAME_FILE_1 = sys.argv[2]
        NAME_FILE_2 = sys.argv[3]
        NUM_NODE = int(sys.argv[4])
        NUM_MOVEMENT_SPEED = int(sys.argv[5])
        NUM_MOVEMENT_RANGE = int(sys.argv[6])
    elif INPUT_COMMOND == "-cm":
        FILE_NUM = int(sys.argv[2])
        NUM_TIMES = int(sys.argv[3])
        NUM_AXIS_X = int(sys.argv[4])
        NUM_AXIS_X_MIN = int(sys.argv[5])
        NUM_AXIS_X_MAX = int(sys.argv[6])
    elif INPUT_COMMOND == "-tcm":
        FILE_NUM = int(sys.argv[2])
        NUM_NODE = int(sys.argv[3])
        NUM_MOVEMENT_SPEED = int(sys.argv[4])
        NUM_MOVEMENT_RANGE = int(sys.argv[5])
    else:
        print("Error: Commond not found, (-v for more information)")
        exit()
except:
    print("Error: Commond params are not enough, (-v for more information)")
    exit()

if INPUT_COMMOND == "-v":
    originData = np.genfromtxt("result_no_et_co.txt",delimiter=" ")
    originData_cube = dc.sheet2cube(originData,3,NUM_TIMES,NUM_SPEED)
    noroute_rate = dc.cubecolAvezip(originData_cube,0)
    etedelay = dc.cubecolAvezip(originData_cube,1)
    control_rate = dc.cubecolAvezip(originData_cube,2)

    Speed_Axis = np.linspace(NUM_MOVEMENT_MINSPEED,NUM_MOVEMENT_MAXSPEED,NUM_SPEED)
    # print(Speed_Axis)

    fig_1 = plt.figure(figsize=(6,3))
    plt.plot(Speed_Axis,noroute_rate,c='red')
    plt.legend(loc='best',labels=['noroute_rate'])
    plt.grid(True)
    plt.xlabel('Speed(m/s)')
    plt.draw()
    plt.pause(3)
    plt.close(fig_1)

    fig_2 = plt.figure(figsize=(6,3))
    plt.plot(Speed_Axis,etedelay,c='blue')
    plt.legend(loc='best',labels=['etedelay'])
    plt.grid(True)
    plt.xlabel('Speed(m/s)')
    plt.draw()
    plt.pause(3)
    plt.close(fig_2)

    fig_3 = plt.figure(figsize=(6,3))
    plt.plot(Speed_Axis,control_rate,c='black')
    plt.legend(loc='best',labels=['control_rate'])
    plt.grid(True)
    plt.xlabel('Speed(m/s)')
    plt.draw()
    plt.pause(3)
    plt.close(fig_3)

    fileName_ahead  =   "OLSR_Original" + \
                        "_n" + str(NUM_NODE) + \
                        "_s" + str(NUM_MOVEMENT_RANGE) + \
                        "_v" + str(NUM_MOVEMENT_MINSPEED) + \
                        "_v" + str(NUM_MOVEMENT_MAXSPEED) + \
                        "_num" + str(NUM_TIMES)

    fileName_norouteRate = fileName_ahead + "_norouteRate.png"
    fileName_eteDelay = fileName_ahead + "_eteDelay.png"
    fileName_controlRate = fileName_ahead + "_controlRate.png"

    fig_1.savefig(fileName_norouteRate,dpi=600,bbox_inches = 'tight')
    fig_2.savefig(fileName_eteDelay,dpi=600,bbox_inches = 'tight')
    fig_3.savefig(fileName_controlRate,dpi=600,bbox_inches = 'tight')

elif INPUT_COMMOND == "-s":
    originData = np.genfromtxt("result_no_et_co.txt",delimiter=" ")
    originData_cube = dc.sheet2cube(originData,3,NUM_TIMES,NUM_RANGE)
    noroute_rate = dc.cubecolAvezip(originData_cube,0)
    etedelay = dc.cubecolAvezip(originData_cube,1)
    control_rate = dc.cubecolAvezip(originData_cube,2)

    Speed_Axis = np.linspace(NUM_MOVEMENT_MINRANGE,NUM_MOVEMENT_MAXRANGE,NUM_RANGE)
    # print(Speed_Axis)

    fig_1 = plt.figure(figsize=(6,3))
    plt.plot(Speed_Axis,noroute_rate,c='red')
    plt.legend(loc='best',labels=['noroute_rate'])
    plt.grid(True)
    plt.xlabel('Range(m)')
    plt.draw()
    plt.pause(3)
    plt.close(fig_1)

    fig_2 = plt.figure(figsize=(6,3))
    plt.plot(Speed_Axis,etedelay,c='blue')
    plt.legend(loc='best',labels=['etedelay'])
    plt.grid(True)
    plt.xlabel('Range(m)')
    plt.draw()
    plt.pause(3)
    plt.close(fig_2)

    fig_3 = plt.figure(figsize=(6,3))
    plt.plot(Speed_Axis,control_rate,c='black')
    plt.legend(loc='best',labels=['control_rate'])
    plt.grid(True)
    plt.xlabel('Range(m)')
    plt.draw()
    plt.pause(3)
    plt.close(fig_3)
    
    fileName_ahead  =   "OLSR_Original" + \
                        "_n" + str(NUM_NODE) + \
                        "_v" + str(NUM_MOVEMENT_SPEED) + \
                        "_s" + str(NUM_MOVEMENT_MINRANGE) + \
                        "_S" + str(NUM_MOVEMENT_MAXRANGE) + \
                        "_num" + str(NUM_TIMES)

    fileName_norouteRate = fileName_ahead + "_norouteRate.png"
    fileName_eteDelay = fileName_ahead + "_eteDelay.png"
    fileName_controlRate = fileName_ahead + "_controlRate.png"

    fig_1.savefig(fileName_norouteRate,dpi=600,bbox_inches = 'tight')
    fig_2.savefig(fileName_eteDelay,dpi=600,bbox_inches = 'tight')
    fig_3.savefig(fileName_controlRate,dpi=600,bbox_inches = 'tight')

elif INPUT_COMMOND == "-c":
    originData = np.genfromtxt(NAME_FILE_1,delimiter=" ")
    originData_cube = dc.sheet2cube(originData,3,NUM_TIMES,NUM_AXIS_X)
    origin_noroute_rate = dc.cubecolAvezip(originData_cube,0)
    origin_etedelay = dc.cubecolAvezip(originData_cube,1)
    origin_control_rate = dc.cubecolAvezip(originData_cube,2)

    advanceData = np.genfromtxt(NAME_FILE_2,delimiter=" ")
    advanceData_cube = dc.sheet2cube(advanceData,3,NUM_TIMES,NUM_AXIS_X)
    advance_noroute_rate = dc.cubecolAvezip(advanceData_cube,0)
    advance_etedelay = dc.cubecolAvezip(advanceData_cube,1)
    advance_control_rate = dc.cubecolAvezip(advanceData_cube,2)

    Axis_X = np.linspace(NUM_AXIS_X_MIN,NUM_AXIS_X_MAX,NUM_AXIS_X)
    Label_X = "Speed(m/s)"

    fig_1 = plt.figure(figsize=(6,3))
    plt.plot(Axis_X,origin_noroute_rate,marker='s',c='red')
    plt.plot(Axis_X,advance_noroute_rate,marker='^',c='blue')
    plt.legend(loc='best',labels=['origin_noroute_rate','advance_noroute_rate'])
    plt.grid(True)
    plt.xlabel(Label_X)
    plt.draw()
    plt.pause(3)
    plt.close(fig_1)

    fig_2 = plt.figure(figsize=(6,3))
    plt.plot(Axis_X,origin_etedelay,marker='s',c='red')
    plt.plot(Axis_X,advance_etedelay,marker='^',c='blue')
    plt.legend(loc='best',labels=['origin_etedelay','advance_etedelay'])
    plt.grid(True)
    plt.xlabel(Label_X)
    plt.draw()
    plt.pause(3)
    plt.close(fig_2)

    fig_3 = plt.figure(figsize=(6,3))
    plt.plot(Axis_X,origin_control_rate,marker='s',c='red')
    plt.plot(Axis_X,advance_control_rate,marker='^',c='blue')
    plt.legend(loc='best',labels=['origin_control_rate','advance_control_rate'])
    plt.grid(True)
    plt.xlabel(Label_X)
    plt.draw()
    plt.pause(3)
    plt.close(fig_3)

    fig_1.savefig('compare_noroute_rate',dpi=600,bbox_inches = 'tight')
    fig_2.savefig('compare_etedelay',dpi=600,bbox_inches = 'tight')
    fig_3.savefig('compare_control_rate',dpi=600,bbox_inches = 'tight')

elif INPUT_COMMOND == "-t":
    originData = np.genfromtxt("result_throughput.txt",delimiter="\t")
    time = originData[:,0]
    throughput = originData[:,1]

    fig_1 = plt.figure(figsize=(6,3))
    plt.plot(time,throughput,c='red')
    plt.legend(loc='best',labels=['throughput'])
    plt.grid(True)
    plt.xlabel('time(s)')

    fileName_ahead  =   "OLSR_Original" + \
                        "_n" + str(NUM_NODE) + \
                        "_v" + str(NUM_MOVEMENT_SPEED) + \
                        "_s" + str(NUM_MOVEMENT_RANGE)
    fileName_throughput = fileName_ahead + "_throughput.png"

    fig_1.savefig(fileName_throughput,dpi=600,bbox_inches = 'tight')

    plt.draw()
    plt.pause(3)
    plt.close(fig_1)

elif INPUT_COMMOND == "-tc":
    originData = np.genfromtxt(NAME_FILE_1,delimiter="\t")
    originData_time = originData[:,0]
    originData_throughput = originData[:,1]

    advanceData = np.genfromtxt(NAME_FILE_2,delimiter="\t")
    advanceData_time = advanceData[:,0]
    advanceData_throughput = advanceData[:,1]

    fig_1 = plt.figure(figsize=(6,3))
    plt.plot(originData_time,originData_throughput,marker='s',c='red')
    plt.plot(advanceData_time,advanceData_throughput,marker='^',c='blue')
    plt.legend(loc='best',labels=['origin_throughput','advance_throughput'])
    plt.grid(True)
    plt.xlabel('time(s)')

    fileName_ahead  =   "compare_throughput" + \
                        "_n" + str(NUM_NODE) + \
                        "_v" + str(NUM_MOVEMENT_SPEED) + \
                        "_s" + str(NUM_MOVEMENT_RANGE)
    fileName_throughput = fileName_ahead + ".png"

    fig_1.savefig(fileName_throughput,dpi=600,bbox_inches = 'tight')

    plt.draw()
    plt.pause(3)
    plt.close(fig_1)
    
elif INPUT_COMMOND == "-cm":
    FILENAMES = []
    for i in range(FILE_NUM):
        FILENAMES.append(sys.argv[i+7])

    Axis_X = np.linspace(NUM_AXIS_X_MIN,NUM_AXIS_X_MAX,NUM_AXIS_X)
    color = ['red','blue','green','cyan','magenta','black','orange','purple','brown','olive']
    dot = 	['s','^','v','o','*','+','x','<','>','D']
	
    fig_1 = plt.figure(figsize=(6,3))
    for i in range(FILE_NUM):
        data = np.genfromtxt(FILENAMES[i],delimiter=" ")
        dataCube = dc.sheet2cube(data,3,NUM_TIMES,NUM_AXIS_X)
        norouteRate = dc.cubecolAvezip(dataCube,0)
        plt.plot(Axis_X,norouteRate,c=color[i],marker=dot[i],linestyle="--")
    plt.legend(loc='best',labels=FILENAMES)
    plt.grid(True)
    plt.xlabel('Speed(m/s)')
    plt.ylabel('norouteRate')
    plt.draw()
    plt.pause(3)
    plt.close(fig_1)

    fig_2 = plt.figure(figsize=(6,3))
    for i in range(FILE_NUM):
        data = np.genfromtxt(FILENAMES[i],delimiter=" ")
        dataCube = dc.sheet2cube(data,3,NUM_TIMES,NUM_AXIS_X)
        etedelay = dc.cubecolAvezip(dataCube,1)
        plt.plot(Axis_X,etedelay,c=color[i],marker=dot[i],linestyle="--")
    plt.legend(loc='best',labels=FILENAMES)
    plt.grid(True)
    plt.xlabel('Speed(m/s)')
    plt.ylabel('etedelay')
    plt.draw()
    plt.pause(3)
    plt.close(fig_2)

    fig_3 = plt.figure(figsize=(6,3))
    for i in range(FILE_NUM):
        data = np.genfromtxt(FILENAMES[i],delimiter=" ")
        dataCube = dc.sheet2cube(data,3,NUM_TIMES,NUM_AXIS_X)
        controlRate = dc.cubecolAvezip(dataCube,2)
        plt.plot(Axis_X,controlRate,c=color[i],marker=dot[i],linestyle="--")
    plt.legend(loc='best',labels=FILENAMES)
    plt.grid(True)
    plt.xlabel('Speed(m/s)')
    plt.ylabel('controlRate')
    plt.draw()
    plt.pause(3)
    plt.close(fig_3)

    fig_1.savefig('compare_noroute_rate',dpi=600,bbox_inches = 'tight')
    fig_2.savefig('compare_etedelay',dpi=600,bbox_inches = 'tight')
    fig_3.savefig('compare_control_rate',dpi=600,bbox_inches = 'tight')

elif INPUT_COMMOND == "-tcm":
    FILENAMES = []
    for i in range(FILE_NUM):
        FILENAMES.append(sys.argv[i+6])

    color = ['red','blue','green','cyan','magenta','black','orange','purple','brown','olive']
    dot = 	['s','^','v','o','*','+','x','<','>','D']
   
    fig_1 = plt.figure(figsize=(6,3))
    for i in range(FILE_NUM):
        data = np.genfromtxt(FILENAMES[i],delimiter="\t")
        time = data[:,0]
        throughput = data[:,1]
        plt.plot(time,throughput,c=color[i],marker=dot[i],linestyle="-")
    plt.legend(loc='best',labels=FILENAMES)
    plt.grid(True)
    plt.xlabel('time(s)')
    plt.ylabel('throughput')
    plt.draw()
    plt.pause(3)
    plt.close(fig_1)

    fileName_ahead  =   "compare_throughput" + \
                        "_n" + str(NUM_NODE) + \
                        "_v" + str(NUM_MOVEMENT_SPEED) + \
                        "_s" + str(NUM_MOVEMENT_RANGE)
    fileName_throughput = fileName_ahead + ".png"

    fig_1.savefig(fileName_throughput,dpi=600,bbox_inches = 'tight')



#/************************ (C) COPYRIGHT 2021 XIWANG**************END OF FILE****/

